package com.ngc.mall.controller;

import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.ActivityDomain;
import com.ngc.mall.service.IActivityService;
import com.ngc.mall.vo.ActivityCouponVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * 活动
 * @author: chenxiaolin
 * @date 2023/8/21 15:11
**/
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    private IActivityService iActivityService;

    @PostMapping("")
    @ApiOperation("新增活动")
    public ResponseResult<Long> createActivity(@RequestBody ActivityDomain domain){
        return ResponseResult.success(iActivityService.createActivity(domain));
    }

    @PutMapping("/id/{id}")
    @ApiOperation("根据id更新活动(只支持编辑【未开始】状态的活动")
    public ResponseResult<Boolean> updateById(@PathVariable Long id,@RequestBody ActivityDomain domain){
        return ResponseResult.auto(iActivityService.updateById(id,domain));
    }

    @GetMapping("/list")
    @ApiOperation("获取所有活动")
    public ResponseResult<List<ActivityDomain>> findAll(){
        return ResponseResult.success(iActivityService.selectAll());
    }

    @PutMapping("/id/{id}/enable")
    @ApiOperation("启用活动")
    public ResponseResult<Boolean> updateActivityStatus(@PathVariable Long id){
        return ResponseResult.auto(iActivityService.updateStatusToDoingById(id));
    }

    @PutMapping("/id/{id}/disable")
    @ApiOperation("停用活动")
    public ResponseResult<Boolean> updateActivityToDisable(@PathVariable Long id){
        return ResponseResult.auto(iActivityService.updateStatusToClosedById(id));
    }

    @GetMapping("/id/{id}")
    @ApiOperation("根据id查看活动")
    public ResponseResult<ActivityCouponVO> getDetailById(@PathVariable Long id){
        return ResponseResult.success(iActivityService.findById(id));
    }

    @DeleteMapping("/id/{id}")
    @ApiOperation("删除活动")
    public ResponseResult<Boolean> deleteActivity(@PathVariable Long id){
        return ResponseResult.auto(iActivityService.deleteById(id));
    }

    @PostMapping("/id/{id}/relate_coupon")
    @ApiOperation("关联优惠券")
    public ResponseResult<Boolean> relateCouponList(@PathVariable Long id,@RequestBody List<Long> couponList){
        return ResponseResult.auto(iActivityService.relateCouponList(id,couponList));
    }

    @PutMapping("/id/{id}/unRelate_coupon")
    @ApiOperation("取消关联优惠券")
    public ResponseResult<Boolean> unRelateCoupon(@PathVariable Long id,@RequestBody Long couponId){
        return ResponseResult.auto(iActivityService.unRelateCouponById(id,couponId));
    }

    @GetMapping("/by/keywords")
    @ApiOperation("根据关键字查看活动")
    public ResponseResult<List<ActivityCouponVO>> findByKeywords(@RequestBody List<String> keywords){
        System.out.println(keywords);
        return ResponseResult.success(iActivityService.findByKeywords(keywords));
    }

}
